/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.editpart;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Reception;

import com.cea.papyrus.core.PapyrusMessages;
import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editor.mouseoveraction.ActionHandleManager;
import com.cea.papyrus.core.editpart.FeatureListEditPart;
import com.cea.papyrus.core.editpart.FeatureListFlowLayoutEditPolicy;
import com.cea.papyrus.core.editpart.FeatureListFlowLayoutEditPolicy.Guard;
import com.cea.papyrus.core.listener.UmlElementListener;
import com.cea.papyrus.core.ui.dialogs.PapyrusMessageDialog;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * A list container accepting Operations.
 * 
 * @author dumoulin
 */
public class OperationListEditPart extends FeatureListEditPart implements EditPart {

	// UML listener
	protected UmlElementListener _adapter ;
	/**
	 * Constructor.
	 * Create a list accepting Property.
	 * 
	 * @param modelManager 
	 * @param graphnode 
	 */
	public OperationListEditPart(GraphNode graphnode, ModelManager modelManager) {
		super(graphnode, org.eclipse.uml2.uml.Operation.class, modelManager);
		_adapter=new UmlElementListener(this);
	}

	public void activate()
	{
		if (isActive()) {
			return;
		}
		super.activate();
		//in order to adapt the accelerator with have to refresh by taking account the changement in class
		if((this.getParent() instanceof ClassEditPart) &&((ClassEditPart)this.getParent()).getUmlElement()!=null){
			((Notifier)((ClassEditPart)this.getParent()).getUmlElement()).eAdapters().add(_adapter);

		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.FeatureListEditPart#deactivate()
	 */
	@Override
	public void deactivate() {
		if (!isActive()) {
			return;
		}

		if((this.getParent() instanceof ClassEditPart) &&((ClassEditPart)this.getParent()).getUmlElement()!=null){
			((Notifier)((ClassEditPart)this.getParent()).getUmlElement()).eAdapters().remove(_adapter);

		}
		
		super.deactivate();
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.FeatureListEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {
		// TODO Auto-generated method stub
		super.refreshVisuals();
//		Install mouse hover
		ActionHandleManager actionMngr = new ActionHandleManager(this);

		PaletteDrawer entries = new PaletteDrawer("creation tool");
		try{
			entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
					PapyrusMessages.AddOperationCommand_Label
					, PapyrusMessages.AddOperationCommand_Description
					, Operation.class
					, null));


			//the accelerator reception is displayed if we have an active class
			if((this.getParent() instanceof InterfaceEditPart)||(this.getParent() instanceof ClassEditPart &&
					((((org.eclipse.uml2.uml.Class)((ClassEditPart)this.getParent()).getUmlElement())).isActive()))
			){

				entries.add(PapyrusPlugin.creationToolFactory.getCombinedTemplateCreationToolEntry(
						PapyrusMessages.AddReceptionCommand_Label
						, PapyrusMessages.AddReceptionCommand_Description
						, Reception.class
						, null));

			}

			actionMngr.addTool(entries, PositionConstants.NORTH_EAST);

		}
		catch (Exception e) {
			PapyrusMessageDialog.display(e.getMessage(), e);
		}
	}
	/*
	 * Add editing capabilities
	 */
	/**
	 * 
	 */
	@Override
	protected void createEditPolicies() {
		// overrides layout edit policy
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new FeatureListFlowLayoutEditPolicy(new OperationGuard()));




	}

	/**
	 * 
	 */
	class OperationGuard implements Guard {

		/* (non-Javadoc)
		 * @see com.cea.papyrus.common.editpart.FeatureListFlowLayoutEditPolicy.Guard#accept(java.lang.Object)
		 */
		/**
		 * 
		 * 
		 * @param object 
		 * 
		 * @return 
		 */
		public boolean accept(Object object) {
			if(object instanceof Operation || object instanceof Reception) {
				return true;
			}
			if(Operation.class.isAssignableFrom((Class)object) || Reception.class.isAssignableFrom((Class)object)) {
				return true;
			}
			return false;
		}

	}
}
